<?php

class model_firadio_php_sys_procphp {

    private $phpcode = '';
    private $tabchar = "\t";
    private $curtab = 0;
    private $newlinechar = "\r\n";

    public function __construct($tabchar, $starttab) {
        $this->tabchar = $tabchar;
        $this->curtab = $starttab;
    }

    public function loadarray($array_procedure) {
        foreach ($array_procedure as $proc) {
            $type = $proc['typename'];
            $id = $proc['id'];
            //echo 'id=' . $id . ',';
            $procsub = isset($proc['sub']) ? $proc['sub'] : array();
            $note = $proc['note'];
            $proc = $proc['proctext'];
            /*
              $parameter = '';
              if (is_array($proc['parameter'])) {
              $parameter = implode(',', $proc['parameter']);
              } else {
              $parameter = $proc['parameter'];
              } */
            $proc['parameter'] = isset($proc['parameter']) ? $proc['parameter'] : '';
            $this->phpcode.=$this->pretab() . '// ' . $note . $this->newlinechar;
            switch ($type) {
                case('newclass'):
                    $this->phpcode.=$this->pretab() . $proc['assignvarname'] . ' = new ' . $proc['class'] . '(' . $proc['parameter'] . ');' . $this->newlinechar;
                    break;
                case('callclass'):
                    $this->phpcode.=$this->pretab();
                    if ($proc['assignvarname'] != '')
                        $this->phpcode.=$proc['assignvarname'] . ' = ';
                    $this->phpcode.=$proc['classvar'] . '->' . $proc['function'] . '(' . $proc['parameter'] . ');' . $this->newlinechar;
                    break;
                case('if'):
                    $this->phpcode.=$this->pretab() . 'if (' . $proc['condition'] . ') {' . $this->newlinechar;
                    if ($procsub) {
                        echo 'sub';
                        $this->curtab++;
                        $this->loadarray($procsub);
                        $this->curtab--;
                    }
                    $this->phpcode.=$this->pretab() . '}' . $this->newlinechar;
                    break;
                case('else'):
                    $this->phpcode.=$this->pretab() . 'else {' . $this->newlinechar;
                    if ($procsub) {
                        echo 'sub';
                        $this->curtab++;
                        $this->loadarray($procsub);
                        $this->curtab--;
                    }
                    $this->phpcode.=$this->pretab() . '}' . $this->newlinechar;
                    break;
                case('classvar'):
                    $this->phpcode.=$this->pretab() . $proc['classvar'] . '->' . $proc['varname'] . ' = ' . $proc['object'] . ';' . $this->newlinechar;
                    break;
            }
        }
    }

    public function pretab() {
        return str_repeat($this->tabchar, $this->curtab);
    }

    public function getphpcode() {
        return($this->phpcode);
    }

}